<template>
  <div class="my-container">
    <div class="header">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>应用授权</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="form">
      <el-form :inline="true">
        <el-form-item>
          <el-button type="primary" round @click="edit(true)">添加</el-button>
        </el-form-item>
        <el-form-item label="appKey">
          <el-input v-model="page.cond.appName" placeholder="按应用名称查询" clearable></el-input>
        </el-form-item>
        <el-form-item label="appKey">
          <el-input v-model="page.cond.appKey" placeholder="按appKey查询" clearable></el-input>
        </el-form-item>
        <el-form-item label="appSecret">
          <el-input v-model="page.cond.appSecret" placeholder="按appSecret查询" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="list(true)">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="content">
      <el-table
        :data="result.records"
        border
        empty-text="暂无数据"
        height="100%">
        <el-table-column type="index" align="center"></el-table-column>
        <el-table-column prop="appName" label="应用名称" min-width="200"></el-table-column>
        <el-table-column prop="appKey" label="appKey" min-width="400"></el-table-column>
        <el-table-column prop="appSecret" label="appSecret" min-width="400"></el-table-column>
        <el-table-column prop="path" label="路径" min-width="400"></el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="100">
          <template #default="scope">
            <el-button @click="edit(null, scope.row)" type="text" link size="small">编辑</el-button>
            <el-button @click="remove(scope.row)" type="text" link size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="footer">
      <el-pagination
        background
        layout="prev, pager, next"
        @current-change="changePage"
        :page-size="result.size"
        :total="result.total">
      </el-pagination>
    </div>
    <!-- 模态框 -->
    <el-dialog
      title="编辑"
      ref="form-dialog"
      v-model="visible.edit"
      :close-on-click-modal="false"
      width="600px">
      <div>
        <el-form ref="form" :model="info" label-width="100px" :rules="rules">
          <el-form-item label="appName" prop="appName">
            <el-input v-model="info.appName" placeholder="请输入应用名称"></el-input>
          </el-form-item>
          <el-form-item label="appKey" prop="appKey">
            <el-input v-model="info.appKey" placeholder="请输入appKey"></el-input>
          </el-form-item>
          <el-form-item label="appSecret" prop="appSecret">
            <el-input v-model="info.appSecret" placeholder="请输入appSecret"></el-input>
          </el-form-item>
          <el-form-item label="path" prop="path">
            <el-input v-model="info.path" placeholder="请输入以/api开始的路径;如: /api/coconut"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
          <span class="dialog-footer">
            <el-button @click="visible.edit = false">取 消</el-button>
            <el-button type="primary" @click="save">确 定</el-button>
          </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Index",
  data() {
    return {
      visible: {
        edit: false
      },
      page: {
        current: 1,
        size: 10,
        total: 0,
        orders: [
          { column: "create_time", asc: false }
        ],
        cond: {
          appName: null,
          appKey: null,
          appSecret: null
        },
      },
      result: {},
      info: {
        id: null,
        appName: null,
        appKey: null,
        appSecret: null,
        path: null
      },
      rules: {
        appName: [
          { required: true, message: "必填" }
        ],
        appKey: [
          { required: true, message: "必填" }
        ],
        appSecret: [
          { required: true, message: "必填" }
        ],
        path: [
          { required: true, message: "必填" }
        ]
      }
    };
  },
  mounted() {
    this.list();
  },
  methods: {
    // 分页查询
    list(first) {
      if (first) {
        this.page.current = 1;
      }
      this.$http.post("/admin/sys/cauth/list", this.page, res => {
        this.result = res.data;
      }, false, true, false);
    },
    // 编辑数据,add为true表示新增
    edit(add, row) {
      this.visible.edit = true;
      this.$nextTick(() => {
        if (add) {
          this.$utils.resetObj(this.info, null);
          this.$refs["form"].resetFields();
        } else {
          this.$utils.copyObj(row, this.info);
        }
      });
    },
    // 保存数据
    save() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.$http.post("/admin/sys/cauth/save", this.info, () => {
            this.visible.edit = false;
            this.list();
          }, true, true, true, this.$refs["form-dialog"].dialogRef);
        }
      });
    },
    remove(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$http.post("/admin/sys/cauth/remove", row, () => {
          this.list();
        }, true, true, true);
      });
    },
    // 当前页切换
    changePage(current) {
      this.page.current = current;
      this.list();
    },
  }
};
</script>

<style lang="scss" scoped>
.my-container {
  padding: 0;
  text-align: left;
  height: 100%;
  overflow: hidden;
  display: flex;
  flex-direction: column;

  .header {
    padding: 20px;
    border-bottom: 1px solid #eeeeee;
    text-align: left;
  }

  .form {
    padding: 20px 20px 0 20px;
    font-size: 14px;
    text-align: left;
  }

  .content {
    flex: 1;
    height: 0;
  }

  .footer {
    padding: 5px;
  }
}
</style>